package com.woniuxy.day11springmvc.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
/**
 * @Author: linKe
 * @Date: 2021/11/24 0:01
 * @FileName: UploadController
 * @version: 1.0
 * @Description: 描述
 */
@Controller
@RequestMapping("/file")
public class UploadController {


        @RequestMapping("/upload")
        public String uploadFile(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam("multipartFile") MultipartFile multipartFile, Model model) throws IOException {
            //获取上传路径
            String realPath = request.getServletContext().getRealPath("/static/images/");
            System.out.println(realPath);
            //获取上传的文件名
            String fileName = multipartFile.getOriginalFilename();
            //创建一个新的文件
            File file = new File(realPath,fileName);
            //判断images文件夹是否存在 如果不存在则创建这个文件夹
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            //复制文件
            multipartFile.transferTo(new File(realPath + File.separator + fileName));
            model.addAttribute("msg", "插入成功路径为" + new File(realPath + File.separator + fileName));
            return "success";
        }


        @RequestMapping("/download")
        public ResponseEntity<byte[]> downLoad(HttpServletRequest request,HttpServletResponse response,
                                               @RequestParam("filename") String filename,
                                               Model model) throws UnsupportedEncodingException {
            //获取下载目录
            String realpath = request.getServletContext().getRealPath("/static/images/");
            //创建文件对象
            File file = new File(realpath + File.separator + filename);
            //创建http头对象
            HttpHeaders httpHeaders = new HttpHeaders();

            //下载显示的文件名,解决中文名称乱码的问题
            String downloadFilename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");

            //通知浏览器以attachment(下载方式)打开图片
            httpHeaders.setContentDispositionFormData("attachment",downloadFilename);
            //application/octet-stream:二进制流数据(最常见的文件下载)
            httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            try {
                //ok是200状态码
                return  new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders, HttpStatus.OK);
            } catch (IOException e) {
                HttpHeaders httpHeader1 = new HttpHeaders();

                httpHeader1.setContentType(MediaType.APPLICATION_JSON);
                //INTERNAL_SERVER_ERROR是500状态码
                return new ResponseEntity("文件名错误!",httpHeader1,HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }

}
